# -*- coding:utf-8 -*-
# @FileName  :custom_class.py
# @Time      :2020/10/25 15:59
# @Author    :zhoujirong

class NbDict(dict):#入参必须是字典

    def __getattr__(self, item): #{"login_time"：1} 当属性和方法不存在会执行__getattr__方法
        value = self.get(item)#字典中取值 1
        if type(value) == dict:
            value = NbDict(value)  # 如果是字典类型，直接返回DictToObject这个类的对象

        elif isinstance(value, list) or isinstance(value, tuple):
            # 如果是list，循环list判断里面的元素，如果里面的元素是字典，那么就把字典转成DictToObject的对象
            value = list(value)
            for index, obj in enumerate(value):
                if isinstance(obj, dict):
                    value[index] = NbDict(obj)
        return value


if __name__ == "__main__":
    d = [{"login_time":1,'name':'test','car':{"baoma":3,"baoshijie":2}}]

    # m = NbDict(d)
    # m.test('name')

    import json
    # for a,b in enumerate(c):
    #     if isinstance(b,dict):
    #         print(True)
    # #     print(a,b)
    #
    # print(type(c))
    # print(list(c))
    # print(type(a))
    # d1 = NbDict(c)
    # d2 = NbDict(d)
    # print(d2.login_time)
    # print(d1)
    # print(d1.archiveid)
